﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Badger.Vhdl;
using Weazel.Badger.Vhdl.Types;
using Weazel.Badger.Vhdl.Statements;
using Weazel.Badger.Vhdl.Expressions;
using Weazel.Badger.Vhdl.Operators;
using Weazel.Badger.Vhdl.Operators.Binary;
using Weazel.Badger.Vhdl.Operators.Unary;

#endregion

namespace Weazel.Badger
{
  public class ProcessSensitivityStatement
  {
    public static IfStatement Get(
      RegisterEntity context,
			Process process,
      StatementCollection resetStatements, 
      StatementCollection clockStatements
    )
    {
      IfStatement ifStatement = new IfStatement();

      SignalReferenceExpression sre =
        new SignalReferenceExpression(
					process.Statements,
					context.ResetPort
				);

      ConstantExpression ce = 
				new ConstantExpression(
					process.Statements,
					"'1'", 
					new StdLogic(), 
					null
				);

      BinaryExpression resetEqlOneExpression =
        new BinaryExpression(
					process.Statements,
          BinaryOperators.Equals, 
          sre, 
          ce, 
          new Vhdl.Types.Boolean(),
          null
        );

      ifStatement.Condition = resetEqlOneExpression;
      ifStatement.ElsIfStatement = new IfStatement();

      BinaryExpression clockEqlOneExpression =
        new BinaryExpression(
					process.Statements,
          BinaryOperators.Equals,
          new SignalReferenceExpression(
						process.Statements,
						context.ClockPort
					),
          new ConstantExpression(
						process.Statements,
						"'1'", 
						new StdLogic(), 
						null
					),
          new Vhdl.Types.Boolean(),
          null
      );

      AttributeExpression clocksEventExpression = 
        new AttributeExpression(
					process.Statements,
					context.ClockPort.EventAttribute
				);

      BinaryExpression andExpression =
        new BinaryExpression(
					process.Statements,
          BinaryOperators.And,
          clocksEventExpression,
          clockEqlOneExpression,
          new Vhdl.Types.Boolean(),
          null
        );

      ifStatement.ElsIfStatement.Condition = andExpression;

      ifStatement.TrueStatements = resetStatements;
      ifStatement.ElsIfStatement.TrueStatements = clockStatements;

      return ifStatement;
    }
  }
}
